//
//  YTHomePreviewVC.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/9.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTHomePreviewVC.h"
#import "YTVideoView.h"
#import "YTRightView.h"
#import "YTRightBackView.h"
#import "YTCamerSetViewController.h"
#import "YTLeftView.h"
#import "ZYXRuler.h"
#import "YTPhotoResourceController.h"
#import "YTGridView.h"
#import "YTSetVC.h"
@interface YTHomePreviewVC ()<YTLeftViewDelegate>
@property(nonatomic, strong) YTVideoView *videoView;
@property(nonatomic, strong) YTLeftView *leftView;
@property (nonatomic, strong) YTRightView *rightView;
@property (nonatomic, strong)  YTRightBackView *rightBackView;
@end

@implementation YTHomePreviewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.userInteractionEnabled = YES;
    [self p_addSubviews];
    [self p_addNotification];
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.videoView.frame = [UIScreen mainScreen].bounds;
    YTGridView *grid = [_videoView viewWithTag:801];
    if (grid)
    {
        grid.frame = _videoView.bounds;
    }
    if([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait||[UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown){
        self.rightBackView.frame = CGRectMake(0, 150, ScreenWidth, ScreenHeight -  self.rightView.height);
        [_leftView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.equalTo(self.view);
            make.height.mas_equalTo(100);
        }];
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.height.equalTo(@(150));
        }];
    }else{
        self.rightBackView.frame = CGRectMake(0, 0, ScreenWidth - 150, ScreenHeight);
        [_leftView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.bottom.equalTo(self.view);
            make.width.mas_equalTo(100);
        }];
        [self.rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.bottom.equalTo(self.view);
            make.right.equalTo(self.view);
            make.width.equalTo(@(150));
        }];
    }
}
- (YTRightBackView *)rightBackView
{
    if(!_rightBackView){
        _rightBackView = [[YTRightBackView alloc] init];
        _rightBackView.previewVC = self;
        _rightBackView.hidden = YES;
        _rightBackView.backgroundColor = YTColorAlpha(74,74,74,0.5);
        IMP_BLOCK_SELF(YTHomePreviewVC);
        _rightBackView.completion = ^(id sender) {
            block_self.rightView.backgroundColor = [UIColor clearColor];
        };
    }
    return _rightBackView;
}
- (YTRightView *)rightView
{
    if(!_rightView){
        _rightView = [YTRightView loadViewForNib];
        IMP_BLOCK_SELF(YTHomePreviewVC);
        _rightView.clickhandler = ^(id sender) {
            if ([sender integerValue] == 0)
            {
                [block_self.navigationController popViewControllerAnimated:YES];
            }else if ([sender integerValue] == 3)
            {
                YTSetVC *setVc = [[YTSetVC alloc] init];
                [block_self.navigationController pushViewController:setVc animated:YES];
            }else
            {
            block_self.rightView.backgroundColor = [UIColor blackColor];
            block_self.rightBackView.index = [sender intValue];
            }
        };
    }
    return _rightView;
}
-(YTVideoView *)videoView
{
    if (!_videoView)
    {
        _videoView = [[YTVideoView alloc] init];
        _videoView.frame = [UIScreen mainScreen].bounds;
    }
    return _videoView;
}
-(YTLeftView *)leftView
{
    if (!_leftView)
    {
        _leftView = [[YTLeftView alloc] initWithVC:self];
        _leftView.delegate = self;
    }
    return _leftView;
}
#pragma mark notification method
-(void)gridNotificationAction:(NSNotification *)noti
{
    if (![_videoView viewWithTag:801] && publicModel.gridModel != 0)
    {
        YTGridView *grid = [[YTGridView alloc] initWithFrame:_videoView.bounds];
        grid.tag = 801;
        [_videoView addSubview:grid];
    }else
    {
        YTGridView *grid = [_videoView viewWithTag:801];
        [grid configGrid];
    }
}
#pragma mark private method
-(void)p_addSubviews
{
    [self.view addSubview:self.videoView];
    [self.view addSubview:self.leftView];
    [self.view addSubview:self.rightBackView];
    [self.view addSubview:self.rightView];
    [self gridNotificationAction:nil];
    Language(@"");
}
-(void)p_addNotification
{
    [NOTIFICATION_CENTER addObserver:self selector:@selector(gridNotificationAction:) name:GridNotification object:nil];
}
#pragma mark YTLeftViewDelegate
//相册
-(void)delegateAlbumAction:(UIButton *)albumBtn
{
    YTPhotoResourceController *plVC = [[YTPhotoResourceController alloc] init];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:plVC];
    
    [self presentViewController:navi animated:YES completion:nil];
}
-(void)dealloc
{
    [NOTIFICATION_CENTER removeObserver:self name:GridNotification object:nil];
}
@end
